package cdu.javaendwork.service.Impl.Shopping;

import cdu.javaendwork.dao.Universal;
import cdu.javaendwork.dao.impl.UniversalImpl;
import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.service.Interface.Shopping.OrderService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    Universal universal = new UniversalImpl();

    @Override
    public boolean insert(int userid, int status, Date date, BigDecimal price, String ref) {
        return universal.insert("Order",userid , status , date,price , ref) == 1;
    }

    @Override
    public boolean delete(int id) {
        return universal.deleteById("Order" , id) == 1;
    }

    @Override
    public boolean update(int id, int userid, int status, Date date, BigDecimal price, String ref) {
        return universal.updateById("Order" , id , userid,status,date,price,ref) == 1;
    }

    //检查该用户的购物车是否存在，在用户创建购物车或者添加商品进入购物车时都会使用,存在返回Order，不存在返回null
    @Override
    public Order findByUserIdAndStatus(int userid, int status) {
        List<Order> list = universal.filter("Order" , "userid="+userid+" and status="+status);
        if(list.isEmpty()) return null;
        else return list.get(0);
    }

    @Override
    public Order findById(int id) {
        return universal.getById("Order" , id);
    }

    @Override
    public List<Order> filterByUserIdLimit(int userid, int pageIndex, int pageSize) {
        return universal.filterLimit("Order" , "userid="+userid+" and status=2",pageIndex,pageSize);
    }
}
